#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>

struct msgbuf {
   long mtype;       /* message type, must be > 0 */
   char mtext[256];    /* message data */
};

int main(int argc, char const *argv[])
{
	key_t mykey = 87886;
	int mqid =0;
	struct msgbuf mybuf,rcvBuf;
	char data[256] = {"massage test!!!"};
	mqid =  msgget(mykey,0666| IPC_CREAT);
	if (mqid < -1)
	{
		printf("failed\n");
		return -1;
	}
	else
	{
		printf("%d\n",mqid);
	}
	mybuf.mtype = 1;
	strcpy(mybuf.mtext,data);
	if(msgsnd(mqid,&mybuf,sizeof(struct msgbuf),IPC_NOWAIT)==-1)
		printf("msgsnd failed\n");
	else
		printf("msgsnd successful\n");

	if (msgrcv(mqid,&rcvBuf,sizeof(struct msgbuf),0,0)==-1)
	{
		printf("rcv failed!!!\n");
	}
	else
	{
		printf("rcv:%s\n", rcvBuf.mtext;
	}
	msgctl(mqid,IPC_RMID,0); //删除消息队列
	return 0;
}